<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺编号" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入店铺编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜品类型编号" prop="dishesTypeId">
        <el-select v-model="queryParams.dishesTypeId" placeholder="请选择菜品类型编号" clearable>
          <el-option
            v-for="dict in dishesTypeList"
            :key="dict.typeId"
            :label="dict.dishesTypeName"
            :value="dict.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="菜品名称" prop="dishesName">
        <el-input
          v-model="queryParams.dishesName"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜品价格" prop="dishesPrice">
        <el-input
          v-model="queryParams.dishesPrice"
          placeholder="请输入菜品价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dishes:dishes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dishes:dishes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dishes:dishes:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dishes:dishes:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleEsTotal"
          v-hasPermi="['dishes:dishes:handleEsTotal']"
        >全量同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dishesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜品编号" align="center" prop="dishesId" />
      <el-table-column label="店铺编号" align="center" prop="storeId" />
      <el-table-column label="菜品类型编号" align="center" prop="dishesTypeId">
      </el-table-column>
      <el-table-column label="菜品名称" align="center" prop="dishesName" >
        <template slot-scope="scope">
          <span v-html="scope.row.dishesName"></span>
        </template>
      </el-table-column>
      <el-table-column label="菜品价格" align="center" prop="dishesPrice" />
      <el-table-column label="菜品图片" align="center" prop="picture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dishes:dishes:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dishes:dishes:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺编号" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺编号" />
        </el-form-item>
        <el-form-item label="菜品类型编号" prop="dishesTypeId">
          <el-select v-model="form.dishesTypeId" placeholder="请选择菜品类型编号">
            <el-option
              v-for="dict in dishesTypeList"
              :key="dict.typeId"
              :label="dict.dishesTypeName"
              :value="dict.typeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜品名称" prop="dishesName">
          <el-input v-model="form.dishesName" placeholder="请输入菜品名称" />
        </el-form-item>
        <el-form-item label="菜品价格" prop="dishesPrice">
          <el-input v-model="form.dishesPrice" placeholder="请输入菜品价格" />
        </el-form-item>
        <el-form-item label="菜品图片" prop="picture">

          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:8080/dishes/dishes/upload"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>


        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      {{form}}
    </el-dialog>
  </div>
</template>

<script>
import { listDishes, getDishes, delDishes, addDishes, updateDishes,getDishesTypeList ,handleEsTotal} from "@/api/dishes/dishes";
import {getToken} from "@/utils/auth";
export default {
  name: "Dishes",
  dicts: ['tb_dishes_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品表格数据
      dishesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        storeId: null,
        dishesTypeId: null,
        dishesName: null,
        dishesPrice: null,
        picture: null
      },
      pageNum: 1,
      pageSize: 10,
      // 表单参数
      form: {
        picture:''
      },
      // 表单校验
      rules: {
      },

      //菜品类型
      dishesTypeList:[],
      imageUrl:'',
      headers:{
        Authorization:''
      }
    };
  },
  created() {
    this.getList();
    getDishesTypeList().then(resp=>{
      this.dishesTypeList=resp.data
    })
    this.headers= {
      'Authorization': getToken()
    }
  },
  methods: {
    /** 查询菜品列表 */
    getList() {
      this.loading = true;
      listDishes(this.queryParams, this.pageNum, this.pageSize).then(response => {
        this.dishesList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dishesId: null,
        storeId: null,
        dishesTypeId: null,
        dishesName: null,
        dishesPrice: null,
        picture: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dishesId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dishesId = row.dishesId || this.ids
      getDishes(dishesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dishesId != null) {
            updateDishes(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDishes(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dishesIds = row.dishesId || this.ids;
      this.$modal.confirm('是否确认删除菜品编号为"' + dishesIds + '"的数据项？').then(function () {
        return delDishes(dishesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dishes/dishes/export', {
        ...this.queryParams
      }, `dishes_${new Date().getTime()}.xlsx`)
    },
    /**
     * 全量同步
     */
    handleEsTotal() {
      handleEsTotal().then(resp => {
        this.$message({
          message: '全量同步成功!!',
          type: 'success'
        });

        this.getList()
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.picture=res.msg
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }

}
};
</script>


<style>
em{
  color: red;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
